package com.ncu.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ncu.Enum.StatusCodeEnum;

/**
 * controller返回类
 * @param <T>
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 提示信息，如果有错误时，前端可以获取该字段进行提示
     */
    private String msg;
    /**
     * 查询到的结果数据，
     */
    private T data;
    public ResponseResult(){

    }
    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public ResponseResult(String msg) {
        this.code = StatusCodeEnum.SUCCESS.getCode();
        this.msg = msg;
    }
    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(T data) {
        this.code=StatusCodeEnum.SUCCESS.getCode();
        this.data=data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功时候的调用
     */
    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<T>(data);
    }
    public static <T> ResponseResult<T> success(String msg) {
        return new ResponseResult<T>(msg);//状态码是200
    }
    public static <T> ResponseResult<T> success(String msg,T data) {
        return new ResponseResult<T>(StatusCodeEnum.SUCCESS.getCode(),msg,data);//状态码是200
    }
    public static <T> ResponseResult<T> success(int code, String msg,T data) {
        return new ResponseResult<T>(code,msg,data);//状态码是200
    }
    public static <T> ResponseResult<T> success() {
        return new ResponseResult<T>();//状态码是200
    }

    /**
     * 失败时候的调用
     */
    public static <T> ResponseResult<T> error(int code, String msg) {
        return new ResponseResult<T>(code, msg);
    }
    public static <T> ResponseResult<T> error(String msg) {
        return new ResponseResult<T>(StatusCodeEnum.FAIL.getCode(), msg);
    }


}
